package com.wyj.demo.ui.activity

import android.annotation.SuppressLint
import android.annotation.TargetApi
import android.graphics.Color
import android.os.Build
import android.view.View
import androidx.annotation.RequiresApi
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.facade.annotation.Route
import com.ashokvarma.bottomnavigation.BottomNavigationBar
import com.custom.base.common.router.RouterPath
import com.custom.base.ui.activity.BaseMvpActivity
import com.custom.util.MiApplication.Companion.context
import com.custom.util.RxBus
import com.custom.util.setBackgroundColor
import com.custom.util.setSrc
import com.custom.util.setVisible
import com.wyj.demo.R
import com.wyj.demo.common.event.SendMessage
import com.wyj.demo.presenter.MainPresenter
import com.wyj.demo.presenter.view.MainView
import com.wyj.demo.ui.fragment.AddressBookFragment
import com.wyj.demo.ui.fragment.FindFragment
import com.wyj.demo.ui.fragment.MessageFragment
import com.wyj.demo.ui.fragment.MyFragment
import kotlinx.android.synthetic.main.main_activity.*
import me.leolin.shortcutbadger.ShortcutBadger
import java.util.*


@Route(path = RouterPath.Activity.PATH_INDEX)
class MainActivity : BaseMvpActivity<MainPresenter>(), MainView {
    //
    @RequiresApi(Build.VERSION_CODES.M)
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    override fun setBarColor() {
        //沉浸式 标题栏文字为黑色
        drawerLayout.systemUiVisibility =
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        // 沉浸式 标题栏文字为白色
//        drawerLayout.systemUiVisibility =
//            View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
        //设置标题栏透明
        window.statusBarColor = Color.TRANSPARENT
        //设置标题内边距
        title_bar.layout.setPadding(0, 60, 0, 0)
        val badgeCount = 1
        ShortcutBadger.applyCount(context, badgeCount) //for 1.1.4+
    }

    override fun setLayout(): Int {
        return R.layout.main_activity
    }

    @SuppressLint("CheckResult")
    override fun initData() {
        initFragment()
        initBottomNav()
        changeFragment(0)
        RxBus.observe<SendMessage>().subscribe {

        }
        //
        //        openWebSocket()

        //        检查登录
        //        if (!isLogin()) {
        //            LoginDialog(this@MainActivity).show()
        //        }
    }

    /**
     * 启动WebSocket连接
     */
    private fun openWebSocket() {
        //        启动Socket
        //        val intent = Intent(this, WsService::class.java)
        //        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        //            startForegroundService(intent)
        //        } else {
        //            startService(intent)
        //        }
    }

    //Fragment 栈管理
    private val mStack = Stack<Fragment>()
    //
    private val messageFragment by lazy { MessageFragment() }    //
    private val addressBookFragment by lazy { AddressBookFragment() }    //
    private val findFragment by lazy { FindFragment() }    //
    private val myFragment by lazy { MyFragment() }    //

    //        初始化Fragment栈管理
    private fun initFragment() {
        val manager = supportFragmentManager.beginTransaction()
        manager.add(R.id.mContainer, messageFragment)
        manager.add(R.id.mContainer, addressBookFragment)
        manager.add(R.id.mContainer, findFragment)
        manager.add(R.id.mContainer, myFragment)
        manager.commit()

        mStack.add(messageFragment)
        mStack.add(addressBookFragment)
        mStack.add(findFragment)
        mStack.add(myFragment)
    }

    //          初始化底部导航切换事件
    private fun initBottomNav() {
        bottom_bar.setTabSelectedListener(object : BottomNavigationBar.OnTabSelectedListener {
            override fun onTabReselected(position: Int) {
                if (position == 1) {
//                    刷新
//                    RxBus.send(RefreshRukuOrderBus(null, true))
                }
            }

            override fun onTabUnselected(position: Int) {
            }

            override fun onTabSelected(position: Int) {
                changeFragment(position)
            }
        })
    }

    //切换Tab，切换对应的Fragment
    private fun changeFragment(position: Int) {
//        miWebSocket.send("早上好.....")
        val manager = supportFragmentManager.beginTransaction()
        for (fragment in mStack) {
            manager.hide(fragment)
        }
        manager.show(mStack[position])
        manager.commit()

        if (position == 3) {
            title_bar.layout.setBackgroundColor("#FFFFFF")
            title_bar.leftText.setVisible(false)
            title_bar.leftImage.setVisible(false)
            title_bar.rightTwoImg.setVisible(false)
            title_bar.rightOneImg.setVisible(false)
            title_bar.titleRightCamera.setVisible(true)
            title_bar.rightOneImg.setSrc(R.mipmap.camera_top)
        } else {
            title_bar.layout.setBackgroundColor("#EEEEEE")
            title_bar.rightOneImg.setSrc(R.mipmap.insert_top)
            title_bar.titleRightCamera.setVisible(false)
            title_bar.leftText.setVisible(true)
            title_bar.rightOneImg.setVisible(true)
            title_bar.rightTwoImg.setVisible(true)
            when (position) {
                0 -> {
                    title_bar.leftText.text = "消息(22)"
                }
                1 -> {
                    title_bar.leftText.text = "通讯录"
                }
                2 -> {
                    title_bar.leftText.text = "发现"
                }
            }

        }
    }


    //注册组件
    override fun initComponent(): MainPresenter {
        return MainPresenter(this)
    }

    override fun onBackPressed() {
        finish()
    }
}
